<template>
  <div class="employee-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <div class="search-label">员工姓名：</div>
      <!-- clearable	是否可清空 -->
      <el-input
        v-model="params.name"
        clearable
        placeholder="请输入内容"
        class="search-main"
        @clear="doSearch"
      />
      <el-button type="primary" @click="doSearch">查询</el-button>
    </div>
    <div class="create-container">
      <el-button type="primary" @click="addEmployee">添加员工</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="employeeList">
        <el-table-column type="index" label="序号" />
        <el-table-column label="员工姓名" prop="name" />
        <el-table-column label="登录账号" prop="userName" />
        <el-table-column label="联系方式" prop="phonenumber" />
        <el-table-column label="角色" prop="roleName" />
        <el-table-column label="状态">
          <!-- 插槽 -->
          <template #default="scope">
            {{ formatStatus(scope.row.status) }}
          </template>
        </el-table-column>
        <el-table-column label="添加时间" prop="createTime" />
        <el-table-column label="操作" fixed="right" width="120">
          <!-- 插槽 -->
          <template #default="scope">
            <el-button
              size="mini"
              type="text"
              @click="edit(scope.row)"
            >编辑</el-button>
            <el-button
              size="mini"
              type="text"
              @click="delEmployee(scope.row.id)"
            >删除</el-button>
            <el-button size="mini" type="text">重置密码</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 底部分页器 -->

    <div class="page-container">
      <el-pagination
        layout="total, prev, pager, next"
        :page-size="params.pageSize"
        :total="total"
        @current-change="pageChange"
      />
    </div>

    <!--  对话框  添加员工 / 编辑更新员工-->
    <el-dialog
      :title="id ? '编辑员工' : '添加员工'"
      width="680px"
      :visible="dialogVisible"
      @close="closeDialog"
      @open="openDialog"
    >
      <!-- 表单接口 -->
      <div class="form-container">
        <el-form
          ref="addForm"
          :model="addForm"
          :rules="addFormRules"
          label-width="80px"
        >
          <el-form-item label="员工姓名" prop="name">
            <el-input v-model="addForm.name" />
          </el-form-item>
          <el-form-item label="登录账号" prop="userName">
            <el-input v-model="addForm.userName" />
          </el-form-item>
          <el-form-item label="联系方式" prop="phonenumber">
            <el-input v-model="addForm.phonenumber" />
          </el-form-item>
          <el-form-item label="分配角色" prop="roleId">
            <el-select v-model="addForm.roleId" placeholder="请选择角色">
              <el-option
                v-for="item in roleList"
                :key="item.id"
                :label="item.roleName"
                :value="item.roleId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="员工状态" prop="status">
            <el-radio-group v-model="addForm.status">
              <el-radio :label="0">禁用</el-radio>
              <el-radio :label="1">启用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button
          size="mini"
          type="primary"
          @click="confirmAdd"
        >确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  getEmployeeListAPI,
  getRoleListAPI,
  createEmployeeAPI,
  delEmployeeAPI,
  editEmployeeAPI
} from '@/api/employee'

export default {
  name: 'Employee', // Employee员工
  data () {
    return {
      employeeList: [], // 渲染列表
      // 列表请求参数
      params: {
        name: '', // 员工姓名
        page: 1,
        pageSize: 4
      },
      total: 0, // 分页总条数

      // ======对话框相关
      dialogVisible: false, // 控制弹框打开关闭
      roleList: [], // 角色列表
      // 对话框中 的 表单对象  ==提供对应的参数
      id: '',
      addForm: {
        name: '',
        userName: '',
        roleId: '',
        status: 1,
        phonenumber: ''
      },
      // 效验规则对象
      addFormRules: {
        name: [{ required: true, message: '请输入员工姓名', trigger: 'blur' }],
        userName: [
          { required: true, message: '请输入登录账号', trigger: 'blur' }
        ],
        phonenumber: [
          { required: true, message: '请输入联系方式', trigger: 'blur' }
        ],
        roleId: [{ required: true, message: '请分配角色', trigger: 'blur' }],
        status: [{ required: true, message: '请选择员工状态', trigger: 'blur' }]
      }
    }
  },
  mounted () {
    // 封装获取列表方法调用
    this.getEmployeeList()
  },
  methods: {
    // 1.获取渲染列表
    async getEmployeeList () {
      // 接口调用
      const res = await getEmployeeListAPI(this.params)
      this.employeeList = res.data.rows
      this.total = res.data.total
    },
    // 2。分页逻辑
    pageChange (page) {
      console.log(page)
      this.params.page = page
      this.getEmployeeList()
    },
    // ===========================
    // 关闭弹框
    closeDialog () {
      this.dialogVisible = false
      // 清除校验痕迹
      this.$refs.addForm.resetFields()
      // 清除数据
      this.addForm = {
        name: '',
        userName: '',
        roleId: '',
        status: 1,
        phonenumber: ''
      }
      this.id = ''
    },
    // 打开弹框
    addEmployee () {
      this.dialogVisible = true
    },
    // 打开弹框 =渲染角色列表
    async openDialog () {
      const res = await getRoleListAPI()
      this.roleList = res.data
    },
    // 添加员工/编辑
    confirmAdd () {
      // 表单统一效验
      this.$refs.addForm.validate(async isOK => {
        console.log(isOK)
        if (isOK) {
          if (this.id) {
            // 1. 调用编辑接口
            await editEmployeeAPI({ id: this.id, ...this.addForm })
          } else {
            // 1. 调用添加接口
            await createEmployeeAPI(this.addForm)
          }
          // 2. 关闭弹框
          this.dialogVisible = false
          this.getEmployeeList()
          this.$message.success(this.addForm ? '编辑成功' : '添加成功')
        }
      })
    },
    //  删除员工
    delEmployee (id) {
      this.$confirm('删除员工后将不可登录，确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await delEmployeeAPI(id)
          this.$message.success('删除成功')
          this.params.page = 1
          this.getEmployeeList()
        })
        .catch(() => {})
    },
    // 员工状态适配
    formatStatus (status) {
      return status === 1 ? '禁用' : '启用'
    },
    // 去搜索
    doSearch () {
      this.params.page = 1
      this.getEmployeeList(this.params.name)
    },
    //  点击编辑时打开弹框并回显数据
    edit (row) {
      console.log(row)
      this.dialogVisible = true
      // 解构每一行的属性
      const { name, userName, roleId, status, phonenumber } = row
      // 赋值给 from表单绑定的对象
      this.id = row.id
      this.addForm = { name, userName, roleId, status, phonenumber }
    }
  }
}
</script>

<style lang="scss" scoped>
.employee-container {
  padding: 10px;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, 0.9);
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.create-container {
  margin: 10px 0px;
}
.page-container {
  padding: 4px 0px;
  text-align: right;
}
.form-container {
  padding: 0px 80px;
}
</style>
